import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:loxen/mode/master/file_info_list.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/image_url.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/widget/photo_view_simple_screen.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/viewmode/file_info_viewmodel.dart';

class FilePictureList extends StatelessWidget {
  const FilePictureList({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ScrollController _scrollController = ScrollController();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
        context.read<FileInfoViewModel>().nextData();
      }
    });
    return Selector<FileInfoViewModel, List<FileInfo>>(
      builder: (context, list, _) {
        List<String> images = list.map((e) => e.cover).toList();
        return Column(
          children: <Widget>[
            Expanded(
              child: GridView.count(
                controller: _scrollController,
                crossAxisCount: 2,
                mainAxisSpacing: 0,
                crossAxisSpacing: 0,
                padding: EdgeInsets.only(left: 36.w, right: 36.w, top: 47.h),
                children: list
                    .map<Widget>((result) => InkWell(
                          onTap: () {
                            bool select = context.read<FileInfoViewModel>().isDelete;
                            if (select) {
                              context.read<FileInfoViewModel>().selectEntity(result);
                            } else {
                              Navigator.push(
                                context,
                                new MaterialPageRoute(
                                    builder: (context) => PhotoViewSimpleScreen(
                                          imagePath: result.cover,
                                          images: images,
                                          heroTag: 'simple',
                                        )),
                              );
                            }
                          },
                          child: Stack(
                            children: <Widget>[
                              Container(
                                width: double.infinity,
                                height: double.infinity,
                                child: CachedNetworkImage(
                                  imageUrl: ImageUrlUtil.getClean(result.cover),
                                  placeholder: (_, __) => Constants.loadingIcon(),
                                  fit: BoxFit.cover,
                                ),
                              ),
                              Align(
                                alignment: Alignment.topRight,
                                child: Builder(
                                    builder: (context) =>
                                        context.watch<FileInfoViewModel>().isDelete
                                            ? Selector<FileInfoViewModel, bool>(
                                                builder: (_, data, __) {
                                                  return Padding(
                                                    padding: EdgeInsets.only(
                                                      top: 20.w,
                                                      right: 20.w,
                                                    ),
                                                    child: data
                                                        ? Image.asset(
                                                            'images/select@2x.png',
                                                            width: 28.w,
                                                            height: 28.w,
                                                          )
                                                        : Container(
                                                            width: 28.w,
                                                            height: 28.w,
                                                            decoration: BoxDecoration(
                                                                color: Theme.of(context)
                                                                    .backgroundColor,
                                                                border: Border.all(
                                                                    color: Colors.black26,
                                                                    width: 0.5)),
                                                          ),
                                                  );
                                                },
                                                selector: (_, __) => result.select == 1)
                                            : Constants.empty),
                              )
                            ],
                          ),
                        ))
                    .toList(),
              ),
            ),
            Builder(
              builder: (context) => context.watch<FileInfoViewModel>().isDelete
                  ? Row(
                      children: <Widget>[
                        Expanded(
                          child: GestureDetector(
                            onTap: () async {
                              bool delete = await showDialog<bool>(
                                  context: context,
                                  builder: (context) {
                                    return AlertDialog(
                                        title: Text(StringResources.ti_shi),
                                        content: Text(
                                          '是否确认移除收藏的图片？',
                                          style: TextStyle(
                                              color: Theme.of(context).textTheme.subtitle2.color),
                                        ),
                                        actions: <Widget>[
                                          FlatButton(
                                            child: Text(StringResources.qu_xiao),
                                            onPressed: () => Navigator.of(context).pop(),
                                          ),
                                          FlatButton(
                                            child: Text(StringResources.shan_chu),
                                            onPressed: () {
                                              Navigator.of(context).pop(true);
                                            },
                                          ),
                                        ]);
                                  });
                              if (delete != null && delete) {
                                context.read<FileInfoViewModel>().deleteData();
                              }
                            },
                            child: Container(
                              width: double.infinity,
                              height: 98.h,
                              decoration: BoxDecoration(
                                color: Color(0xfff2f2f2),
//                                border: Border.all(
//                                    color: Colors.black26, width: 0.5),
                              ),
                              child: Center(
                                child: Row(
                                  mainAxisSize: MainAxisSize.min,
                                  children: <Widget>[
//                                    Image.asset(
//                                      'images/trash_can.png',
//                                      width: 32.w,
//                                      height: 32.w,
//                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(left: 19.w),
                                      child: Text(StringResources.shan_chu),
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              context.read<FileInfoViewModel>().isDelete = false;
                            },
                            child: Container(
                              width: double.infinity,
                              height: 98.h,
                              decoration: BoxDecoration(
                                color: Color(0xff0cb7d9),
//                                border: Border.all(
//                                    color: Colors.black26, width: 0.5),
                              ),
                              child: Center(
                                child: Text(
                                  StringResources.qu_xiao,
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    )
                  : Constants.empty,
            )
          ],
        );
      },
      selector: (_, model) => model.list,
    );
  }
}
